#coding:utf-8

import os, sys
sys.path.append(".")
from utils import *
from jieba_wrapper import *

lang_words_dict = {}
lang_words_dict['粤语'] = 1
lang_words_dict['英文'] = 1
lang_words_dict['英文原版'] = 1
lang_words_dict['闽南语'] = 1
lang_words_dict['广东话'] = 1
lang_words_dict['福建话'] = 1
lang_words_dict['英文版'] = 1
lang_words_dict['潮汕话'] = 1
lang_words_dict['潮汕语'] = 1
lang_words_dict['英语版'] = 1
lang_words_dict['英语'] = 1
lang_words_dict['台语'] = 1
lang_words_dict['客家话'] = 1
lang_words_dict['美语版'] = 1
lang_words_dict['雷州话'] = 1
lang_words_dict['维吾尔语'] = 1
lang_words_dict['维吾尔版'] = 1
lang_words_dict['吴语版'] = 1
lang_words_dict['永安话'] = 1
lang_words_dict['江淮话'] = 1
lang_words_dict['兰州话'] = 1
lang_words_dict['美语'] = 1
lang_words_dict['韩语版'] = 1
lang_words_dict['日语版'] = 1
lang_words_dict['日文版'] = 1
lang_words_dict['韩文版'] = 1
lang_words_dict['朝鲜话'] = 1
lang_words_dict['东北话'] = 1
lang_words_dict['法语版'] = 1
lang_words_dict['德国语'] = 1
lang_words_dict['法国语'] = 1
lang_words_dict['德语版'] = 1
lang_words_dict['东北话'] = 1
lang_words_dict['东北话版'] = 1

tags_dict = {}
tags_dict['animation'] = '动画'
tags_dict['动漫'] = '动画'
tags_dict['卡通'] = '动画'
tags_dict['颁奖典礼'] = '颁奖礼'
tags_dict['春节晚会'] = '春晚'
tags_dict['婚姻'] = '婚恋'
tags_dict['暗恋'] = '婚恋'
tags_dict['重口'] = '重口味'
tags_dict['网络剧'] = '网剧'
tags_dict['action'] = '动作'
tags_dict['kids'] = '少儿'
tags_dict['儿童'] = '少儿'
tags_dict['family'] = '家庭'
tags_dict['sports'] = '运动'
tags_dict['体育'] = '运动'
tags_dict['tv'] = '电视'
tags_dict['电影版'] = '电影'
tags_dict['院线'] = '电影'
tags_dict['国内院线'] = '电影'
tags_dict['网络院线'] = '电影'
tags_dict['新片速递'] = '电影'
tags_dict['新片'] = '电影'
tags_dict['大片'] = '电影'
tags_dict['佳片'] = '电影'
tags_dict['新番动画'] = '动画'
tags_dict['国漫'] = '动画'
tags_dict['微影院'] = '微电影'
tags_dict['美剧'] = '美国'
tags_dict['悲情'] = '悲剧'
tags_dict['付费影片'] = '电影'

bad_tags = {'暂无':1, '其他':1, '其它':1, '未知':1, '缺':1, '不详':1, '待定':1}

def format_category( line ):
    result = []
    if not line:
        return []
    if isinstance( line, unicode):
        line = line.encode('utf-8', 'ignore')
    if not isinstance( line, str):
        return []
    if line in bad_tags:
        return []

    line = line.lower().replace('，', ';')
    line = line.replace('|',';').replace('/', ';').replace('、', ';').replace("'",'').replace(',', ";").replace('；', ';')
    tags = line.split(';')
    for tag in tags:
        tag = tag.strip()
        if tag == '': continue
        tag = tag.replace('中国', '').strip()
        if tag == '频道': 
            continue
        if tag.endswith('地区'): tag = tag[:-6]
        elif tag.endswith('时期'): tag = tag[:-6]
        elif tag.endswith('专题'): tag = tag[:-6]
        elif tag.endswith('专区'): tag = tag[:-6]
        elif tag.endswith('以上'): tag = tag[:-6]
        elif tag.endswith('以下'): tag = tag[:-6]
        elif len(tag) > 6 and tag.endswith('版'): tag = tag[:-3]
        elif len(tag) > 6 and tag.endswith('剧'): tag = tag[:-3]
        utag = tag.decode('utf-8', 'ignore')

        if len(utag) <= 1: continue
        if tag in bad_tags: continue

        num = len(tag)
        if tag.endswith('岁') :
            result.append( tag[:-3])
            continue
        if tag.endswith('分钟') :
            result.append( tag[:-3])
            continue

        if tag in tags_dict:
            result.append( tags_dict[tag] )
            continue

        if num <= 6:
            result.append( tag )
            continue
            
        if num == 9:
            if tag.endswith('剧'):
                tag = tag[:-3]
            elif tag.endswith('版'):
                tag = tag[:-3]
            elif tag.endswith('片'):
                tag = tag[:-3]
            result.append( tag )
            continue

        words = cutwords( tag )
        for word in words:
            if word['flag'] == 'w': continue
            if word['flag'] == 'x': continue
            word = word['word'].strip()
            if word == '': continue
            if word.endswith('剧'): word = word[:-3]
            if word.endswith('版'): word = word[:-3]
            if len(word) > 6  and word.endswith('片'): word = word[:-3]
            if len( word ) == 12 and word.endswith('频道'):
                word = word[:-6]
            if len( word ) == 12 and word.endswith('节目'):
                result.append( '节目')
                word = word[:-6]
            elif len( word ) == 12 and word.endswith('电影'):
                result.append( '电影')
                word = word[:-6]
            uword = word.decode('utf-8', 'ignore')
            if len(uword) <= 1: continue
            if word in tags_dict:
                result.append( tags_dict[word] )
                continue

            if len(uword) == 4 and len(word) == 12:
                w1 = uword[:2].encode('utf-8', 'ignore')
                w2 = uword[2:].encode('utf-8', 'ignore')
                result.append( w1 )
                result.append( w2 )
                continue

            result.append( word )

    result = list(set(result))
    return result


def score_category(album, video):
    if not album:
        return 0
    if not video:
        return 0
    if isinstance(album, unicode):
        album = album.encode('utf-8', 'ignore')
    if isinstance(video, unicode):
        video = video.encode('utf-8', 'ignore')
    if not isinstance(album, str):
        return 0
    if not isinstance(video, str):
        return 0
    #album = album.replace('暂无', '').replace('其它', '').replace('未知','').replace('其他', '').replace('待定', '').replace(',', ';').replace('/', ';')
    #video = video.replace('暂无', '').replace('其它', '').replace('未知','').replace('其他', '').replace('待定', '').replace(',', ';').replace('/', ';')
    if not album:
        return 0
    if not video:
        return 0
    #album = album.replace('|',';').replace('/', ';').replace('、', ';').replace("'",'')
    #video = video.replace("|",';').replace('/', ';').replace('、', ';').replace("'",'')
    #albums = album.split(';')
    #videos = video.split(';')

    albums = format_category( album )
    videos = format_category( video )

    albums = list( set(albums) )
    videos = list( set(videos) )
    if len(videos) <= 0: 
        return 0
    if len(albums) <= 0: 
        return 0

    allnum = len(albums) + len(videos)
    allnum /= 2.0

    com = set(albums).intersection(set(videos))
    tot = set(albums).union(set(videos))
    diff = list(tot.difference( com ) )

    ###电影、动漫、综艺、记录片、短片..........
    ratio = len(com) / allnum
    if ratio > 0.001:
        return ratio

    if ratio < 0.001:
        if '短片' in diff:
            return -1
        elif '微电影' in diff:
            return -1
        elif '微影院' in diff:
            return -1
        elif '动画' in diff:
            return -1
        elif '片花' in diff:
            return -1
        elif '片段' in diff:
            return -1
        elif '电影' in diff and '电视' in diff:
            return -1
        elif '电影' in diff and '电视剧' in diff:
            return -1
        elif '综艺' in diff and '电影' in diff:
            return -1
        elif '记录' in diff and '电影' in diff:
            return -1

    return ratio


def score_area( album, video ):
    if not album:
        return 0
    if not video:
        return 0

    if isinstance(album, unicode):
        album = album.encode('utf-8', 'ignore')
    if isinstance(video, unicode):
        video = video.encode('utf-8', 'ignore')
    if not isinstance(album, str):
        return 0
    if not isinstance(video, str):
        return 0

    xareas = areaTool.format_areastr( album )
    yareas = areaTool.format_areastr( video )

    xareas = list( set(xareas) )
    yareas = list( set(yareas) )
    if len(xareas) <= 0:
        return 0
    if len(yareas) <= 0:
        return 0
    com = list(set(xareas).intersection(set(yareas)))
    tot = list(set(xareas).union(set(yareas)))
    if len(com) > 0:
        return 1
    if len(com) <= 0:
        return -1
    return 0

def score_lang( album, video ):
    if not album:
        return 0
    if not video:
        return 0
    if isinstance(album, unicode):
        album = album.encode('utf-8', 'ignore')
    if isinstance(video, unicode):
        video = video.encode('utf-8', 'ignore')
    if not isinstance(album, str):
        return 0
    if not isinstance(video, str):
        return 0

    xlangs = langTool.format_langstr( album )
    ylangs = langTool.format_langstr( video )

    xlangs = list( set(xlangs) )
    ylangs = list( set(ylangs) )
    if len(xlangs) <= 0: 
        return 0
    if len(ylangs) <= 0: 
        return 0
    com = list(set(xlangs).intersection(set(ylangs)))
    tot = list(set(xlangs).union(set(ylangs)))
    if len(com) > 0:
        return 1
    if len(com) <= 0:
        return -1
    return 0

def test_langs():
    f = open('bin/testcase/langs.txt', 'rb')
    lines = f.readlines()
    f.close()
    num = len(lines)
    #print ','.join( langTool.format_langstr('英语  法语'))
    #sys.exit()
    for i in range(num-1):
        x = lines[i].strip()
        y = lines[i+1].strip()
        xlangs = langTool.format_langstr( x )
        print '###',x
        #print '###',y
        #print score_lang(x, y)
        for la in xlangs:
            print la
        print
        pass
    #score_lang()

def test_areas():
    f = open('bin/testcase/areas.txt', 'rb')
    lines = f.readlines()
    f.close()
    num = len(lines)
    #print ','.join( langTool.format_langstr('英语  法语'))
    #sys.exit()
    for i in range(num-1):
        x = lines[i].strip()
        y = lines[i+1].strip()
        #xlangs = areaTool.format_areastr( x )
        print '###',x
        print '###',y
        print score_area(x, y)
        #for la in xlangs:
        #    print la
        print

def test_cate():
    f = open('bin/testcase/cate_names.txt', 'rb')
    lines = f.readlines()
    f.close()
    num = len(lines)
    for i in range(num-1):
        x = lines[i].strip()
        y = lines[i+1].strip()
        print '###',x
        print '###', y
        print score_category(x, y)
        print 


if __name__ == "__main__":
    f = open('etc/tags.txt' , 'rb')
    for line in f:
        tags = format_category(line )
        if not tags: continue
        print '\n'.join( tags )
        
    f.close()
    pass
    test_langs()
    test_areas()
    test_cate()

